using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using DTO;

namespace DAL
{
   public  class tbloaigiaodichDAL
    {
         private SqlConnection cn = null;
        public tbloaigiaodichDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
         public tbloaigiaodichDAL(tbloaigiaodichDAL giaodich )
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }
        public void Insert(tbloaigiaodich  giaodich)
        {
            SqlCommand cm = new SqlCommand("loaigiaodich_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@tenloaigiaodich", giaodich .Tenloaigiaodich   );
            cm.ExecuteNonQuery();
        }
        public void Update(tbloaigiaodich giaodich)
        {
            SqlCommand cm = new SqlCommand("loaigiaodich_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@maloaigiaodich", giaodich .Maloaigiaodich );
            cm.Parameters.AddWithValue("@tenloaigiaodich", giaodich .Tenloaigiaodich );
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("loaigiaodich_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@maloaigiaodich", iD);
            cm.ExecuteNonQuery();
        }
        public tbloaigiaodichcollection Get(string where, int pageIndex, int pageSize,
           string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("loaigiaodich_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbloaigiaodichcollection loaigiaodichCollection = new tbloaigiaodichcollection();
            while (dr.Read())
            {
               tbloaigiaodich giaodich = new tbloaigiaodich();
               giaodich .Maloaigiaodich = int.Parse(dr.GetValue(0).ToString());
               giaodich .Tenloaigiaodich   = (dr.GetValue(1).ToString());
               loaigiaodichCollection   .Add(giaodich  );
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return loaigiaodichCollection    ;
        }
    }
}
